﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DSS_Simplex
{
    class Mesin
    {
        public static void CariMax(double[,] table)
        {
            int i, j = 0, s, n;
            double calc;
            int baris, kolom;
            double tanda;
            double min, max;
            int flag = 0;

            baris = table.GetLength(0);
            kolom = table.GetLength(1);
            double[] tanda2 = new double[baris - 1];

            for (i = 1; i < kolom; i++)
            {
                if (table[0, i] <= 0)
                    flag = 0;
                if (table[0, i] > 0)
                {
                    flag = 1;
                    break;
                }
            }
            while (flag == 1)
            {
                max = table[0, 1];
                s = 1;

                Console.WriteLine("%%%%%%%%%%%%%%%%%%%");

                for (i = 1; i < kolom; i++)
                {
                    if (max < table[0, i])
                    {
                        max = table[0, i];
                        s = i;
                    }
                }
                min = table[1, 0] / table[1, s];
                j = 1;

                for (i = 1; i < baris; i++)
                {
                    tanda2[i - 1] = table[i, 0] / table[i, s];
                    if (min > tanda2[i - 1])
                    {
                        min = tanda2[i - 1];
                        j = i;
                    }
                }
                tanda = table[j, s];
                for (i = 0; i < kolom; i++)
                {
                    table[j, i] /= tanda;
                }
                for (i = 0; i < baris; i++)
                {
                    if (i != j)
                    {
                        calc = table[i, s] / table[j, s];
                        for (n = 0; n < kolom; n++)
                        {
                            table[i, n] -= table[j, n] * calc;
                        }
                    }
                }
                for (i = 1; i < kolom; i++)
                {
                    if (table[0, i] <= 0)
                        flag = 0;
                    if (table[0, i] > 0)
                    {
                        flag = 1;
                        break;
                    }
                }
            }
        }

        public static void CariMin(double[,] table)
        {
            int i, j = 0, s, n;
            double calc;
            int baris, kolom;
            double tanda;
            double min, max;
            int flag = 0;

            baris = table.GetLength(0);
            kolom = table.GetLength(1);
            double[] tanda2 = new double[baris - 1];

            for (i = 1; i < kolom; i++)
            {
                if (table[0, i] >= 0)
                    flag = 0;
                if (table[0, i] < 0)
                {
                    flag = 1;
                    break;
                }
            }
            while (flag == 1)
            {
                max = table[0, 1];
                s = 1;

                Console.WriteLine("%%%%%%%%%%%%%%%%%%%");

                for (i = 1; i < kolom; i++)
                {
                    if (max < table[0, i])
                    {
                        max = table[0, i];
                        s = i;
                    }
                }
                min = table[1, 0] / table[1, s];
                j = 1;

                for (i = 1; i < baris; i++)
                {
                    tanda2[i - 1] = table[i, 0] / table[i, s];
                    if (min < tanda2[i - 1])
                    {
                        min = tanda2[i - 1];
                        j = i;
                    }
                }
                tanda = table[j, s];
                for (i = 0; i < kolom; i++)
                {
                    table[j, i] /= tanda;
                }
                for (i = 0; i < baris; i++)
                {
                    if (i != j)
                    {
                        calc = table[i, s] / table[j, s];
                        for (n = 0; n < kolom; n++)
                        {
                            table[i, n] -= table[j, n] * calc;
                        }
                    }
                }
                for (i = 1; i < kolom; i++)
                {
                    if (table[0, i] <= 0)
                        flag = 0;
                    if (table[0, i] > 0)
                    {
                        flag = 1;
                        break;
                    }
                }
            }

        }

        public static void InnerProduct(ref double[,] Mtable, int A1, int S2, int A2)
        {
            int i, j;
            int VarAll = S2 + A2 + A1;
            int TCol = Mtable.GetLength(1);
            double[] VectorA = new double[VarAll];
            double[] coefVar = new double[TCol];
            double sum = 0;

            for (i = 0; i < S2; i++)
            {
                VectorA[i] = 0;
            }
            for (i = S2; i < S2 + A1; i++)
            {
                VectorA[i] = 1.0E30;
            }
            for (i = S2 + A1; i < VarAll; i++)
            {
                VectorA[i] = 1.0E30;
            }
            //sum product
            for (j = 0; j < TCol; j++)
            {
                for (i = 0; i < VectorA.Length; i++)
                {
                    sum += VectorA[i] * Mtable[i + 1, j];
                }
                coefVar[j] = sum - Mtable[0, j];
                sum = 0;
            }
            //ganti dgn var baru
            for (i = 0; i < TCol; i++)
            {
                Mtable[0, i] = coefVar[i];
            }

        }
    }
}
